package com.eig.course.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.entity.course.EduCourse;
import com.eig.common.entity.course.EduSubject;
import com.eig.common.entity.system.TbMember;
import com.eig.common.vo.Result;
import com.eig.course.dao.CoreCourseDao;
import com.eig.course.dao.CoreDao;
import com.eig.course.dao.MemberListDao;
import com.eig.course.service.CoreCourseService;
import com.eig.course.vo.EduCourseVo;
import com.eig.course.vo.EduSubjectVo;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2022/12/08 下午 04:39
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CoreCourseServiceImpl implements CoreCourseService {

    @Resource
    private CoreCourseDao coreCourseDao;

    @Resource
    private CoreDao coreDao;

    @Override
    public Result queryByPage(EduCourse eduCourse, PageRequest pageRequest) {
        long total = coreCourseDao.count(eduCourse);
        PageImpl<EduCourse> eduSubjectsL = new PageImpl<>(this.coreCourseDao.queryAllByLimit(eduCourse, pageRequest), pageRequest, total);

        Map<String, Object> map = new HashMap<>();
        LinkedList<EduSubject> eduSubjects = coreDao.queryAlls();

        for (EduCourse course : eduSubjectsL.getContent()) {
            for (EduSubject eduSubject : eduSubjects) {
                if (course.getSubjectId().equals(eduSubject.getId())) {
                    course.setSubjectName("【" + eduSubject.getTitle() + "】");
                }
            }
        }
        for (EduCourse course : eduSubjectsL.getContent()) {
            for (EduSubject eduSubject : eduSubjects) {
                if (course.getSubjectParentId().equals(eduSubject.getId())) {
                    if (!course.getSubjectParentId().equals("0")) {
                        course.setSubjectName(("【" + eduSubject.getTitle() + "】 >  ") + course.getSubjectName());
                    }
                }
            }
        }
        map.put("records", eduSubjectsL.getContent());
        map.put("total", eduSubjectsL.getTotalElements());
        return new Result(2000, "查询成功", map);
    }

    @Override
    public Result insert(EduCourse eduCourse) {
        return null;
    }

    @Override
    public Result changeStatus(EduCourseVo eduCourse) {
        if (eduCourse.getStatus().equals("Normal")) {
            eduCourse.setTitle("0");
        } else {
            eduCourse.setTitle("1");
        }
        coreCourseDao.changeStatus(eduCourse);
        return new Result(2000, "修改成功");
    }

    @Override
    @Transactional
    public Result updateCourse(EduCourseVo eduCourseVo) {
        EduCourse eduCourse = new EduCourse();
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", eduCourseVo.getId());
        if (ObjectUtil.isNotEmpty(eduCourseVo.getTitle())) {
            updateWrapper.set("title", eduCourseVo.getTitle());
        }
        if (ObjectUtil.isNotEmpty(eduCourseVo.getPrice())) {
            updateWrapper.set("price", eduCourseVo.getPrice());
        }
        if (ObjectUtil.isNotEmpty(eduCourseVo.getStatus())) {
            updateWrapper.set("status", eduCourseVo.getStatus());
        }
        if (ObjectUtil.isNotEmpty(eduCourseVo.getDescription())) {
            coreCourseDao.updateDesc(eduCourseVo);
        }
        coreCourseDao.update(eduCourse, updateWrapper);
        return new Result(2000, "修改成功");
    }

    @Override
    public boolean deleteById(String id) {
        return false;
    }

    @Override
    public Result updateCourseById(EduCourse e) {
        int i = coreCourseDao.updateCourseById(e);
        return new Result(2000, "修改成功", i);
    }

    @Resource
    private  MemberListDao memberListDao;
    @Override
    public Result<IPage<TbMember>> queryMemberList(Integer current, Integer pageSize, TbMember tbMember) {
        IPage<TbMember> page = new Page(current, pageSize);
        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        IPage<TbMember> iPage = memberListDao.selectPage(page, wrapper);
        List<TbMember> records = iPage.getRecords();
        if (StringUtils.hasText(tbMember.getMobile())) {
            String id = tbMember.getMobile();
            records = records.stream().filter(index -> index.getMobile().contains(id)).collect(Collectors.toList());
        }
        if (StringUtils.hasText(tbMember.getNickname())) {
            String name = tbMember.getNickname();
            records = records.stream().filter(index -> index.getNickname().contains(name)).collect(Collectors.toList());
            System.out.println(records.toString());
        }
        if (tbMember.getIsDisabled() != null) {
            Integer isDisabled = tbMember.getIsDisabled();
            records = records.stream().filter(index -> index.getIsDisabled() == isDisabled).collect(Collectors.toList());
        }

//        if (tbMember.getGmtCreates() != null) {
//            if (tbMember.getGmtCreates() != null && tbMember.getGmtCreates().size() > 0) {
//                String gmtCreate = tbMember.getGmtCreates().get(0);
//                String gmt_modified = tbMember.getGmtCreates().get(1);
//                // 转换时间格式
//                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                // 让String转换时间类型
//                LocalDateTime parsedDate = LocalDateTime.parse(gmtCreate, formatter);
//                LocalDateTime parsedDate1 = LocalDateTime.parse(gmt_modified, formatter);
//
//                records = records.stream().filter(index -> index.getGmtCreate().isAfter(parsedDate) && index.getGmtCreate().isBefore(parsedDate1)).collect(Collectors.toList());
//
//            }
//        }

        iPage.setRecords(records);
        return new Result<>(2000, "查询成功", iPage);
    }
}
